<template>
  <view class="user-service-stats-container">
    <z-paging 
      ref="paging" 
      v-model="dataList" 
      @query="queryList"
      :auto="false"
      :refresher-enabled="true"
      :loading-more-enabled="true"
      :empty-view-text="'暂无数据'"
      class="list-paging"
    >
      <template #top>
        <!-- 页面标题 -->
        <view class="page-header">
          <view class="header-title">用户与服务统计</view>
        </view>
        
        <!-- 筛选区域 -->
        <view class="filter-section">
          <view class="filter-row">
            <view class="filter-item">
              <text class="filter-label">开始日期：</text>
              <picker mode="date" :value="filters.startDate" @change="onStartDateChange">
                <view class="date-picker">
                  <text>{{ filters.startDate || '请选择' }}</text>
                  <up-icon name="arrow-down" size="14"></up-icon>
                </view>
              </picker>
            </view>
            
            <view class="filter-item">
              <text class="filter-label">结束日期：</text>
              <picker mode="date" :value="filters.endDate" @change="onEndDateChange">
                <view class="date-picker">
                  <text>{{ filters.endDate || '请选择' }}</text>
                  <up-icon name="arrow-down" size="14"></up-icon>
                </view>
              </picker>
            </view>
          </view>
          
          <view class="filter-actions">
            <button class="search-btn" @click="searchData">查询</button>
            <button class="reset-btn" @click="resetFilters">重置</button>
          </view>
        </view>
        
        <!-- 列表表头 -->
        <view class="list-header">
          <text class="list-cell">用户昵称</text>
          <text class="list-cell">用户手机号</text>
          <text class="list-cell">申诉次数</text>
          <text class="list-cell">咨询次数</text>
          <text class="list-cell">求助次数</text>
        </view>
      </template>
      
      <!-- 列表内容 -->
      <view v-for="(item, index) in dataList" :key="index" class="list-row" @click="showDetail(item)">
        <text class="list-cell">{{ item.nickname }}</text>
        <text class="list-cell">{{ item.phone }}</text>
        <text class="list-cell">{{ item.appealCount }}</text>
        <text class="list-cell">{{ item.consultCount }}</text>
        <text class="list-cell">{{ item.helpCount }}</text>
      </view>
    </z-paging>
    
    <!-- 详情弹窗 -->
    <view class="detail-modal" v-if="showDetailModal">
      <view class="detail-content">
        <view class="detail-header">
          <text class="detail-title">用户服务详情</text>
          <text class="close-btn" @click="showDetailModal = false">×</text>
        </view>
        
        <view class="detail-body" v-if="currentDetail">
          <view class="detail-item">
            <text class="detail-label">用户昵称：</text>
            <text class="detail-value">{{ currentDetail.nickname }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">用户手机号：</text>
            <text class="detail-value">{{ currentDetail.phone }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">申诉次数：</text>
            <text class="detail-value">{{ currentDetail.appealCount }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">咨询次数：</text>
            <text class="detail-value">{{ currentDetail.consultCount }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">求助次数：</text>
            <text class="detail-value">{{ currentDetail.helpCount }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">退款次数：</text>
            <text class="detail-value">{{ currentDetail.refundCount }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">二次申诉次数：</text>
            <text class="detail-value">{{ currentDetail.secondAppealCount }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">注册时间：</text>
            <text class="detail-value">{{ currentDetail.registerTime }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">最近服务时间：</text>
            <text class="detail-value">{{ currentDetail.lastServiceTime }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">服务满意度：</text>
            <text class="detail-value">{{ currentDetail.satisfaction }}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue'

// 筛选条件
const filters = reactive({
  startDate: '',
  endDate: ''
})

// 列表数据
const dataList = ref([])
const paging = ref(null)

// 详情弹窗
const showDetailModal = ref(false)
const currentDetail = ref(null)

// 日期选择处理
const onStartDateChange = (e) => {
  filters.startDate = e.detail.value
}

const onEndDateChange = (e) => {
  filters.endDate = e.detail.value
}

// 查询数据
const searchData = () => {
  if (paging.value) {
    paging.value.reload()
  }
}

// 重置筛选条件
const resetFilters = () => {
  filters.startDate = ''
  filters.endDate = ''
  
  nextTick(() => {
    if (paging.value) {
      paging.value.reload()
    }
  })
}

// 查询列表数据
const queryList = (pageNo, pageSize) => {
  // 模拟请求延迟
  setTimeout(() => {
    // 生成模拟数据
    const data = generateMockData(pageNo, pageSize)
    paging.value.complete(data)
  }, 500)
}

// 生成模拟数据
const generateMockData = (pageNo, pageSize) => {
  const data = []
  const startIndex = (pageNo - 1) * pageSize
  
  // 用户昵称
  const nicknames = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十', '郑十一', '王十二']
  
  // 生成随机数据
  for (let i = 0; i < pageSize; i++) {
    const index = startIndex + i
    if (index >= 100) break // 模拟总数限制
    
    // 生成随机用户昵称
    const nickname = nicknames[Math.floor(Math.random() * nicknames.length)]
    
    // 生成随机手机号
    const phone = '1' + ['3', '5', '7', '8', '9'][Math.floor(Math.random() * 5)] + 
                 Math.floor(Math.random() * 100000000).toString().padStart(8, '0')
    
    // 生成随机服务次数
    const appealCount = Math.floor(Math.random() * 10)
    const consultCount = Math.floor(Math.random() * 15)
    const helpCount = Math.floor(Math.random() * 8)
    const refundCount = Math.floor(Math.random() * 5)
    const secondAppealCount = Math.floor(Math.random() * appealCount)
    
    // 生成注册时间（最近2年内的随机日期）
    const registerDate = new Date()
    registerDate.setDate(registerDate.getDate() - Math.floor(Math.random() * 730)) // 最近2年
    const registerTime = formatDate(registerDate)
    
    // 生成最近服务时间
    const lastServiceDate = new Date()
    lastServiceDate.setDate(lastServiceDate.getDate() - Math.floor(Math.random() * 90)) // 最近3个月
    const lastServiceTime = formatDate(lastServiceDate)
    
    // 生成服务满意度
    const satisfactionLevels = ['非常满意', '满意', '一般', '不满意', '非常不满意']
    const satisfactionWeights = [0.4, 0.3, 0.2, 0.07, 0.03] // 权重分布
    
    let satisfaction = ''
    const rand = Math.random()
    let cumulativeWeight = 0
    
    for (let j = 0; j < satisfactionLevels.length; j++) {
      cumulativeWeight += satisfactionWeights[j]
      if (rand <= cumulativeWeight) {
        satisfaction = satisfactionLevels[j]
        break
      }
    }
    
    data.push({
      id: index + 1,
      nickname,
      phone,
      appealCount,
      consultCount,
      helpCount,
      refundCount,
      secondAppealCount,
      registerTime,
      lastServiceTime,
      satisfaction
    })
  }
  
  return data
}

// 格式化日期
const formatDate = (date) => {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

// 显示详情
const showDetail = (item) => {
  currentDetail.value = item
  showDetailModal.value = true
}

// 页面加载
onMounted(() => {
  // 设置默认日期范围（最近7天）
  const endDate = new Date()
  const startDate = new Date(endDate.getTime() - 6 * 24 * 60 * 60 * 1000)
  
  filters.endDate = formatDate(endDate)
  filters.startDate = formatDate(startDate)
  
  // 页面挂载时获取数据
  nextTick(() => {
    if (paging.value) {
      paging.value.reload()
    }
  })
})
</script>

<style lang="scss" scoped>
.user-service-stats-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
}

.list-paging {
  flex: 1;
  height: 100%;
}

.page-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40rpx 30rpx 30rpx;
  color: white;
  
  .header-title {
    font-size: 36rpx;
    font-weight: bold;
    text-align: center;
  }
}

.filter-section {
  background: white;
  margin: 20rpx;
  padding: 30rpx;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  
  .filter-row {
    display: flex;
    margin-bottom: 20rpx;
    
    &:last-child {
      margin-bottom: 0;
    }
  }
  
  .filter-item {
    flex: 1;
    margin-right: 20rpx;
    
    &:last-child {
      margin-right: 0;
    }
    
    &.full-width {
      flex: none;
      width: 100%;
      margin-right: 0;
    }
  }
  
  .filter-label {
    font-size: 28rpx;
    color: #333;
    margin-bottom: 10rpx;
    display: block;
  }
  
  .date-picker,
  .picker-input {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20rpx;
    border: 2rpx solid #e0e0e0;
    border-radius: 10rpx;
    background: #fafafa;
    font-size: 26rpx;
    color: #666;
  }
  
  .filter-actions {
    display: flex;
    gap: 20rpx;
    margin-top: 30rpx;
  }
  
  .search-btn,
  .reset-btn {
    flex: 1;
    padding: 20rpx;
    border-radius: 10rpx;
    font-size: 28rpx;
    border: none;
  }
  
  .search-btn {
    background: #1890FF;
    color: white;
  }
  
  .reset-btn {
    background: #f0f0f0;
    color: #666;
  }
}

.list-header {
  display: flex;
  background: #f0f0f0;
  padding: 20rpx;
  margin: 0 20rpx;
  border-radius: 10rpx 10rpx 0 0;
  font-size: 26rpx;
  font-weight: bold;
  color: #333;
}

.list-row {
  display: flex;
  background: white;
  padding: 20rpx;
  margin: 0 20rpx;
  border-bottom: 2rpx solid #f0f0f0;
  font-size: 26rpx;
  color: #666;
  
  &:last-child {
    border-radius: 0 0 10rpx 10rpx;
    margin-bottom: 20rpx;
  }
}

.list-cell {
  flex: 1;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.detail-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.detail-content {
  width: 80%;
  max-height: 80%;
  background: white;
  border-radius: 20rpx;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  background: #f5f5f5;
  border-bottom: 2rpx solid #e0e0e0;
  
  .detail-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
  }
  
  .close-btn {
    font-size: 40rpx;
    color: #999;
    line-height: 1;
  }
}

.detail-body {
  padding: 30rpx;
  overflow-y: auto;
}

.detail-item {
  display: flex;
  margin-bottom: 20rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  .detail-label {
    width: 200rpx;
    font-size: 28rpx;
    color: #666;
  }
  
  .detail-value {
    flex: 1;
    font-size: 28rpx;
    color: #333;
  }
}
</style>